/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xdlinkdemo.device.message.client;

import com.xuexiang.xdlink.component.message.impl.base.XResponseMessage;
import com.xuexiang.xdlinkdemo.device.Constants;
import com.xuexiang.xdlinkdemo.entity.ApiEntity;
import com.xuexiang.xutil.net.JSONUtils;

/**
 * 客户端json响应消息
 *
 * @author xuexiang
 * @since 2018/9/3 下午8:18
 */
public class ClientJsonResponseMessage extends XResponseMessage<ApiEntity> {

    @Override
    protected byte initFuncCode() {
        return Constants.FunctionCode.CLIENT_JSON_RESPONSE;
    }

    @Override
    protected ApiEntity transformMessageData(byte[] contentData) {
        String json = new String(contentData);
        String type = JSONUtils.getString(json, ApiEntity.KEY_TYPE, "");
        int code = JSONUtils.getInt(json, ApiEntity.KEY_CODE, -1);
        String msg = JSONUtils.getString(json, ApiEntity.KEY_MSG, "");
        String data = JSONUtils.getString(json, ApiEntity.KEY_DATA, "");

        return new ApiEntity(type)
                .setCode(code)
                .setMsg(msg)
                .setData(data);
    }
}
